#include <stdio.h>
#include <stdlib.h>
#include "floatimage.H"

floatimage diffuse;
floatimage normal;

FILE* fp;

/************************************************************************/
/*		
		For elder lady:
			1) without makeup:
				forehead:	
					372		610		294		930
				nose:
					734		1016	562		724
				mouth:
					1100	1402	222		1042
			2) with makeup 1:
				forehead:	
					446		692		370		1004
				nose:
					780		1088	636		794
				mouth:
					1172	1500	276		1094
			3) with makeup 2:
				forehead:	
					502		736		330		996
				nose:
					734		1154	600		758
				mouth:
					1232	1580	304		1078
			
		For younger lady:
			1) without makeup:
				forehead:	
					482		706		358		982
				nose:
					834		1090	632		766
				mouth:
					1158	1444	292		1020
			2) with makeup 1:
				forehead:	
					348		598		348		924
				nose:
					642		976		610		752
				mouth:
					1058	1360	294		978
			3) with makeup 2:
				forehead:	
					502		736		330		996
				nose:
					734		1154	600		758
				mouth:
					1232	1580	304		1078

		For little lady 1:
			1) without makeup:
				forehead:	
					614		835		382		998
				nose:
					954		1266	618		784
				mouth:
					1340	1646	370		1004

		For little lady 2:
			1) without makeup:
				forehead:	
					638		828		396		998
				nose:
					920		1216	642		786
				mouth:
					1280	1602	418		994

		For little lady 3:
			1) without makeup:
				forehead:	
					398		618		304		924
				nose:
					710		1034	568		728
				mouth:
					1092	1384	348		892
																		*/
/************************************************************************/

float region[9][12] = {
						{372, 610, 294, 930,
						734, 1016, 562, 724,
						1100, 1402, 222, 1042},

						{446, 692, 370, 1004,
						780, 1088, 636, 794,
						1172, 1500, 276, 1094},

						{502, 736, 330, 996,
						734, 1154, 600, 758,
						1232, 1580, 304, 1078},

						{482, 706, 358, 982,
						834, 1090, 632, 766,
						1158, 1444, 292, 1020},

						{348, 598, 348, 924,
						642, 976, 610, 752,
						1058, 1360, 294, 978},

						{502, 736, 330, 996,
						734, 1154, 600, 758,
						1232, 1580, 304, 1078},




						{614,	835,	382,	998,
						954,	1266,	618,	784,
						1340,	1646,	370,	1004},

						{638,	828,	396,	998,
						920,	1216,	642,	786,
						1280,	1602,	418,	994},

						{398,	618,	304,	924,
						710,	1034,	568,	728,
						1092,	1384,	348,	892},
						};


void main(int argc, char *argv[])
{

	diffuse.LoadPFM("./data_input/young4/bare/flash_diffWB.pfm");
	normal.LoadPFM("./data_input/young4/bare/diff_normal_b.pfm");

	fp = fopen("./data_output/young4/free/data_f_b.m","w");

	int issue=8;
	int index = 2;

	vec3f l(0,0,1);

	for (int j=region[issue][0];j<region[issue][1];j++)
	{
		for (int i=region[issue][2];i<region[issue][3];i++)
		{
			vec3f n(normal.line(j)[i].r,normal.line(j)[i].g,normal.line(j)[i].b);
			vec3f intensity(diffuse.line(j)[i].r,diffuse.line(j)[i].g,diffuse.line(j)[i].b);

			float c = n * l;
			float theta = 180.0*acosf(c)/3.1415926;

			if (intensity[0]<1e-4||intensity[0]>1e1)
			{
				continue;
			}

			fprintf(fp,"%f %f\n",theta,intensity[index]);
			//t.x = FlowImage.line(j)[i].r + (float)i + 0.5;
			//t.y = FlowImage.line(j)[i].g + (float)j + 0.5;
			//OutputImage.line(j)[i] = InputImage.GetPixelBilinear(t.x, t.y);
		}
	}

	for (int j=region[issue][4];j<region[issue][5];j++)
	{
		for (int i=region[issue][6];i<region[issue][7];i++)
		{
			vec3f n(normal.line(j)[i].r,normal.line(j)[i].g,normal.line(j)[i].b);
			vec3f intensity(diffuse.line(j)[i].r,diffuse.line(j)[i].g,diffuse.line(j)[i].b);

			float c = n * l;
			float theta = 180.0*acosf(c)/3.1415926;

			if (intensity[0]<1e-4||intensity[0]>1e1)
			{
				continue;
			}

			fprintf(fp,"%f %f\n",theta,intensity[index]);
			//t.x = FlowImage.line(j)[i].r + (float)i + 0.5;
			//t.y = FlowImage.line(j)[i].g + (float)j + 0.5;
			//OutputImage.line(j)[i] = InputImage.GetPixelBilinear(t.x, t.y);
		}
	}

	for (int j=region[issue][8];j<region[issue][9];j++)
	{
		for (int i=region[issue][10];i<region[issue][11];i++)
		{
			vec3f n(normal.line(j)[i].r,normal.line(j)[i].g,normal.line(j)[i].b);
			vec3f intensity(diffuse.line(j)[i].r,diffuse.line(j)[i].g,diffuse.line(j)[i].b);

			float c = n * l;
			float theta = 180.0*acosf(c)/3.1415926;

			if (intensity[0]<1e-4||intensity[0]>1e1)
			{
				continue;
			}

			fprintf(fp,"%f %f\n",theta,intensity[index]);
			//t.x = FlowImage.line(j)[i].r + (float)i + 0.5;
			//t.y = FlowImage.line(j)[i].g + (float)j + 0.5;
			//OutputImage.line(j)[i] = InputImage.GetPixelBilinear(t.x, t.y);
		}
	}

	fclose(fp);

}